<?php


namespace app\shop\controller\admin;


use app\shop\controller\BaseAdminController;
use app\shop\model\config\TypeBusinessModel;
use app\shop\model\config\TypeDesignModel;
use app\shop\model\config\TypeFieldModel;
use app\shop\model\config\TypeProgramModel;
use app\shop\model\order\OrderModel;
use app\shop\model\user\UserModel;
use app\shop\validate\OrderValidate;

class FinanceController extends BaseAdminController
{

    public function setModel()
    {
        return new OrderModel();
    }

    function setSearch()
    {
        $where = [];
        $isRefund = $this->request->param('is_refund', '', 'trim');
        if ($isRefund == 0) {
            $where[] = ['order_status', '=', 4];
        }
        if ($isRefund == 1) {
            $where[] = ['is_refund', '=', 1];
        }
        $isDueRefund = $this->request->param('is_due_refund', '', 'trim');
        if ($isDueRefund == 0) {
            $orderIdArr = OrderModel::where('order_status', 2)
                ->where('due_in_refund', 1)
                ->where('due_is_refund', 0)
                ->column('id');
            $orderIdArr1 = OrderModel::where('order_status', 1)
                ->where('due_in_refund', 1)
                ->where('due_is_refund', 0)
                ->where('forbid_refund', 1)
                ->column('id');
            $idArr = array_merge($orderIdArr, $orderIdArr1);
            $where[] = ['id', 'in', $idArr];
        }
        if ($isDueRefund == 1) {
            $where[] = ['due_is_refund', '=', 1];
        }
        $residueAmount = $this->request->param('residue_amount', '', 'trim');
        if ($residueAmount == 0) {
            $where[] = ['residue_amount', '>', 0];
            $where[] = ['is_take', '=', 0];
        }
        if ($residueAmount == 1) {
            $where[] = ['is_take', '=', 1];
        }
        return [
            'like' => ['order_sn', 'mobile'],
            'between_time' => ['create_time'],
            '=' => ['is_open'],
            'custom' => $where
        ];
    }

    function setSort()
    {
        // TODO: Implement setSort() method.
    }

    /**
     * 订单列表
     * @return array|\think\response\Json
     */
    public function getList()
    {
        try {
            $list = parent::getList();
            foreach ($list['data'] as &$v) {
                $v['design_type_name'] = TypeDesignModel::where('id', $v['design_type'])->value('name', '');
                $v['business_type_name'] = TypeBusinessModel::where('id', $v['business_type'])->value('name', '');
                $v['program_name'] = TypeProgramModel::where('id', $v['program_id'])->value('name', '');
                $v['order_status_txt'] = OrderModel::getOrderTxt($v['order_status']);
                $v['design_name'] = UserModel::where('user_id', $v['design_id'])->value('realname', '');
                $v['sale_name'] = UserModel::where('user_id', $v['sale_id'])->value('realname', '');
                $v['create_time'] = date('Y-m-d', strtotime($v['create_time']));
            }
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 添加订单
     * @param array $data
     * @return \think\response\Json
     */
    public function save(array $data = [])
    {
        try {


            return $this->success();
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function del()
    {
        return parent::del(); // TODO: Change the autogenerated stub
    }

    /**
     * 获取订单状态
     * @return \think\response\Json
     */
    public function getOrderStatusArr()
    {
        try {
            $list = OrderModel::getOrderStatusArr();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * 获取订单备选字段
     * @return \think\response\Json
     */
    public function getOrderFieldArr()
    {
        try {
            $list = TypeFieldModel::whereNotNull('name')->select()->toArray();
            return $this->success($list);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    public function setNamespace()
    {
        // TODO: Implement setNamespace() method.
    }

    public function setWithModel()
    {
        // TODO: Implement setWithModel() method.
    }
}